package com.zadtek.shopmall.activities;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsActivity;
import com.zadtek.shopmall.bean.CategoryItem;
import com.zadtek.shopmall.bean.HomeBaseBean;
import com.zadtek.shopmall.bean.OperateResponseBean3;
import com.zadtek.shopmall.bean.ShopDetailProductItem;
import com.zadtek.shopmall.bean.ShopDetailBean;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.other.Const;
import com.zadtek.shopmall.other.LocationManger;
import com.zadtek.shopmall.utils.ToastUtil;
import com.zadtek.shopmall.view.DrawableTextView;
import com.zadtek.shopmall.view.GridSpacingItemDecoration;
import com.zadtek.shopmall.view.dialog.BaseNiceDialog;
import com.zadtek.shopmall.view.dialog.NiceDialog;
import com.zadtek.shopmall.view.dialog.ViewConvertListener;
import com.zadtek.shopmall.view.dialog.ViewHolder;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ShopDetailActivity extends AbsActivity implements View.OnClickListener {
    private AppBarLayout appbar;
    private CollapsingToolbarLayout collapsingToolbar;
    private ImageView ivShopImg;
    private LinearLayout llShopDesc;
    private TextView tvShopName;
    private TextView tvShopInfo;
    private LinearLayout llShopDetail;
    private TextView tvProductMark;
    private TextView tvProductText;
    private TextView tvServiceMark;
    private TextView tvServiceText;
    private TextView tvTimeMark;
    private TextView tvTimeText;
    private DrawableTextView tvCollect;
    private DrawableTextView tvConnect;
    private TextView tvShopDesc;
    private DrawableTextView tvShopAddress;
    private Toolbar title;
    private TextView tvTitle;
    private ImageView icBack;
    private ImageView rightIcon;
    private TextView tvNewProduct;
    private TextView tvHotProduct;
    private TextView tvPrice;
    private ImageView ivMenu;
    private RecyclerView recycleview;
    private CircleImageView shopAvatar;

    private final static int NEW_PRODUCT = 1;
    private final static int HOT_PRODUCT = 2;
    private final static int PRICE_DOWN = 3;
    private final static int PRICE_UP = 4;

    private int currentSelect = NEW_PRODUCT;
    private ShopDetailAdapter shopDetailAdapter;
    private ShopDetailBean bean = new ShopDetailBean();

    private List<ShopDetailProductItem> newProductData = new LinkedList<>();
    private List<ShopDetailProductItem> hotProductData = new LinkedList<>();
    private List<ShopDetailProductItem> priceDownData = new LinkedList<>();
    private List<ShopDetailProductItem> priceUpData = new LinkedList<>();

    private int newProductPage, hotProductPage, priceDownPage, priceUpPage;

    private boolean canLoadMoreNew,canLoadMoreHot,canLoadMoreDown,canLoadMoreUp;

    private int pageSize;

    private CategoryItem nowCategory = new CategoryItem();

    private int shopId;

    public static final String SHOP_ID = "shopid";
    private void initView() {
        appbar = (AppBarLayout)findViewById( R.id.appbar );
        collapsingToolbar = (CollapsingToolbarLayout)findViewById( R.id.collapsing_toolbar );
        ivShopImg = (ImageView)findViewById( R.id.iv_shop_img );
        llShopDesc = (LinearLayout)findViewById( R.id.ll_shop_desc );
        tvShopName = (TextView)findViewById( R.id.tv_shop_name );
        tvShopInfo = (TextView)findViewById( R.id.tv_shop_info );
        llShopDetail = (LinearLayout)findViewById( R.id.ll_shop_detail );
        tvProductMark = (TextView)findViewById( R.id.tv_product_mark );
        tvProductText = (TextView)findViewById( R.id.tv_product_text );
        tvServiceMark = (TextView)findViewById( R.id.tv_service_mark );
        tvServiceText = (TextView)findViewById( R.id.tv_service_text );
        tvTimeMark = (TextView)findViewById( R.id.tv_time_mark );
        tvTimeText = (TextView)findViewById( R.id.tv_time_text );
        tvCollect = (DrawableTextView)findViewById( R.id.tv_collect );
        tvConnect = (DrawableTextView)findViewById( R.id.tv_connect );
        tvShopDesc = (TextView)findViewById( R.id.tv_shop_desc );
        tvShopAddress = (DrawableTextView)findViewById( R.id.tv_shop_address );
        title = (Toolbar)findViewById( R.id.title );
        tvTitle = (TextView)findViewById( R.id.tv_title );
        icBack = (ImageView)findViewById( R.id.ic_back );
        rightIcon = (ImageView)findViewById( R.id.right_icon );
        tvNewProduct = (TextView)findViewById( R.id.tv_new_product );
        tvHotProduct = (TextView)findViewById( R.id.tv_hot_product );
        tvPrice = (TextView)findViewById( R.id.tv_price );
        ivMenu = (ImageView)findViewById( R.id.iv_menu );
        recycleview = (RecyclerView)findViewById( R.id.recycleview );
        shopAvatar = findViewById(R.id.cv_avatar);
    }

    @Override
    public int setView() {
        return R.layout.activity_shop_detail;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        Intent intent = getIntent();
        shopId = intent.getIntExtra(SHOP_ID,-1);
        if(shopId == -1){
            ToastUtil.showToast("illegal shopId !");
        }
        resetAllFlag();
        initView();
        initLogic();
        if(LocationManger.location==null){
            requestData("");
        }else{
            requestData(LocationManger.location.getCity());
        }
    }

    private void resetAllFlag() {
        newProductPage = 1;
        hotProductPage = 1;
        priceDownPage = 1;
        priceUpPage = 1;

        newProductData.clear();
        hotProductData.clear();
        priceDownData.clear();
        priceUpData.clear();

        canLoadMoreNew = true;
        canLoadMoreHot = true;
        canLoadMoreUp = true;
        canLoadMoreDown = true;

        pageSize = 10;
    }

    private void requestData(String city) {

        Map<String, Object> params = NetTool.newParams();
        params.put("shop_id",shopId);
        params.put("address", city);
        NetTool.getApi().getShopInfoData(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<ShopDetailBean>>(this) {
                    @Override
                    public void onData(BaseResponse<ShopDetailBean> value) {
                        bean = value.getData();
                        nowCategory.setText("全部");
                        nowCategory.setCategoryId(-1);
                        nowCategory.setSelected(true);
                        //bean.getCategoryItems().add(0,nowCategory);
                        upDateView();
                    }
                });
//
//        bean.setShopName("办公宝旗舰店");
//        bean.setShopBannerBg("http://pic.qiantucdn.com/58pic/17/86/50/76c58PICbVx_1024.jpg!/fw/780/watermark/url/L3dhdGVybWFyay12MS4zLnBuZw==/align/center");
//        bean.setShopAvatar("http://pic.qiantucdn.com/58pic/17/86/50/76c58PICbVx_1024.jpg!/fw/780/watermark/url/L3dhdGVybWFyay12MS4zLnBuZw==/align/center");
//        bean.setEvaulateMark(5);
//        bean.setServiceMark(3);
//        bean.setTimeMark(4);
//        bean.setLikeCounts(10);
//        bean.setShopDistance("789km");
//        bean.setShopDesc("办公宝旗舰店 职业专营家的路上看见分厘卡圣诞节分厘卡时间的");
//
//        CategoryItem categoryItem1 = new CategoryItem();
//        categoryItem1.setText("全部");
//        categoryItem1.setSelected(true);
//        List<CategoryItem> categoryList = new LinkedList<>();
//        categoryList.add(categoryItem1);
//        CategoryItem categoryItem2 = new CategoryItem();
//        categoryItem2.setText("手机");
//        categoryList.add(categoryItem2);
//        CategoryItem categoryItem3 = new CategoryItem();
//        categoryItem3.setText("电器");
//        categoryList.add(categoryItem3);
//
//        bean.setCategoryItems(categoryList);
//
//        BaseResponse<ShopDetailBean> baseResponse = new BaseResponse<>();
//        baseResponse.setData(bean);
//        baseResponse.setCode(0);
//        baseResponse.setMessage("");
//        Gson gson = new Gson();
//        String json = gson.toJson(baseResponse);
//
//        BaseResponse<List<ShopDetailProductItem>> baseResponse2 = new BaseResponse<>();
//        ShopDetailProductItem indexProductItem = new ShopDetailProductItem();
//        indexProductItem.setProductImgUrl("http://tc.zadtek.com/aaa/images/201603/thumb_img/531_thumb_G_1457132885920.jpg");
//        indexProductItem.setProductPrice("￥90.00");
//        indexProductItem.setProductTitle("申士（SNSIR）SN系 家庭影院套装 5.1组合功放音响 回音壁挂电视音箱 铝合金黑色");
//        indexProductItem.setProductSales("销量 999");
//        indexProductItem.setProductStock("库存 999");
//        indexProductItem.setType(Const.PRODUCT);
//        indexProductItem.setId(454353);
//
//        ShopDetailProductItem indexProductItem2 = new ShopDetailProductItem();
//        indexProductItem2.setProductImgUrl("http://tc.zadtek.com/aaa/images/201603/thumb_img/528_thumb_G_1457132421645.jpg");
//        indexProductItem2.setProductPrice("4000.00");
//        indexProductItem2.setProductTitle("佳仁按摩椅 JR-Q3豪华3D家用按摩椅 旗舰款(贵族红)");
//        indexProductItem2.setProductSales("销量 999");
//        indexProductItem2.setProductStock("库存 999");
//        indexProductItem2.setType(Const.PRODUCT);
//        indexProductItem2.setId(4354644);
//
//        newProductData.add(indexProductItem);
//        newProductData.add(indexProductItem2);
//        newProductData.add(indexProductItem);
//        newProductData.add(indexProductItem2);
//
//        baseResponse2.setData(newProductData);
//        baseResponse2.setCode(0);
//        baseResponse2.setMessage("");
//        Gson gson2 = new Gson();
//        String json2 = gson.toJson(baseResponse2);
//        upDateView();

        switch (currentSelect){
            case NEW_PRODUCT:
                loadNewProductData(this);
                break;
            case PRICE_DOWN:
                loadPriceDownData(this);
                break;
            case HOT_PRODUCT:
                loadHotProduct(this);
                break;
            case PRICE_UP:
                loadPriceUpData(this);
                break;
        }
    }

    private void loadHotProduct(BaseObserver.DialogHelper helper) {
        Map<String, Object> params = NetTool.newParams();
        params.put("sort_type",1);
        params.put("category",nowCategory.getCategoryId());
        params.put("page",hotProductPage);
        params.put("pagesize",pageSize);
        params.put("shop_id",shopId);
        NetTool.getApi().getShopListData(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<ShopDetailProductItem>>>(helper) {
                    @Override
                    public void onData(BaseResponse<List<ShopDetailProductItem>> value) {
                        if(hotProductData.size()>=20){
                            canLoadMoreHot = false;
                            shopDetailAdapter.loadMoreEnd();
                        }else{
                            hotProductPage++;
                            hotProductData.addAll(value.getData());
                            shopDetailAdapter.loadMoreComplete();
                            shopDetailAdapter.notifyDataSetChanged();
                        }
                    }
                });
    }

    private void loadPriceDownData(BaseObserver.DialogHelper helper) {
        Map<String, Object> params = NetTool.newParams();
        params.put("sort_type",3);
        params.put("category",nowCategory.getCategoryId());
        params.put("page",priceDownPage);
        params.put("pagesize",pageSize);
        params.put("shop_id",shopId);
        NetTool.getApi().getShopListData(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<ShopDetailProductItem>>>(helper) {
                    @Override
                    public void onData(BaseResponse<List<ShopDetailProductItem>> value) {
                        if(priceDownData.size()>=20){
                            canLoadMoreDown = false;
                            shopDetailAdapter.loadMoreEnd();
                        }else{
                            priceDownPage++;
                            priceDownData.addAll(value.getData());
                            shopDetailAdapter.loadMoreComplete();
                            shopDetailAdapter.notifyDataSetChanged();
                        }
                    }
                });

    }

    private void loadPriceUpData(BaseObserver.DialogHelper helper) {
        Map<String, Object> params = NetTool.newParams();
        params.put("sort_type",2);
        params.put("category",nowCategory.getCategoryId());
        params.put("page",priceUpPage);
        params.put("pagesize",pageSize);
        params.put("shop_id",shopId);
        NetTool.getApi().getShopListData(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<ShopDetailProductItem>>>(helper) {
                    @Override
                    public void onData(BaseResponse<List<ShopDetailProductItem>> value) {
                        if(priceUpData.size()>20){
                            canLoadMoreUp = false;
                            shopDetailAdapter.loadMoreEnd();
                        }else{
                            priceUpPage++;
                            priceUpData.addAll(value.getData());
                            shopDetailAdapter.loadMoreComplete();
                            shopDetailAdapter.notifyDataSetChanged();
                        }
                    }
                });
    }


    private void loadNewProductData(BaseObserver.DialogHelper helper) {
        Map<String, Object> params = NetTool.newParams();
        params.put("sort_type",0);
        params.put("category",nowCategory.getCategoryId());
        params.put("page",newProductPage);
        params.put("pagesize",pageSize);
        params.put("shop_id",shopId);
        NetTool.getApi().getShopListData(params)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new BaseObserver<BaseResponse<List<ShopDetailProductItem>>>(helper) {
                @Override
                public void onData(BaseResponse<List<ShopDetailProductItem>> value) {
                    if(newProductData.size() >= 20){
                        canLoadMoreNew = false;
                        shopDetailAdapter.loadMoreEnd();
                    }else{
                        newProductPage++;
                        newProductData.addAll(value.getData());
                        shopDetailAdapter.loadMoreComplete();
                        shopDetailAdapter.notifyDataSetChanged();
                    }
                }
            });
    }

    private void upDateView() {
        tvTitle.setText(bean.getShopName());
        Glide.with(this).load(bean.getShopBannerBg()).placeholder(R.drawable.placeholder).into(ivShopImg);
        Glide.with(this).load(bean.getShopAvatar()).placeholder(R.drawable.placeholder).into(shopAvatar);
        tvShopName.setText(bean.getShopName());
        if(bean.getShopDistance()!=null){
            tvShopInfo.setText(bean.getLikeCounts()+"人关注    距离"+bean.getShopDistance());
        }else{
            tvShopInfo.setText(bean.getLikeCounts()+"人关注");
        }
        tvShopDesc.setText(bean.getShopDesc());
        tvShopAddress.setText(bean.getShopAddress());
        tvProductMark.setText(bean.getEvaulateMark()+"分");
        tvServiceMark.setText(bean.getServiceMark()+"分");
        tvTimeMark.setText(bean.getTimeMark()+"分");
        if(bean.isFocus()){
            tvCollect.setText("已关注");
            tvCollect.setClickable(false);
        }

    }

    private void initLogic() {
        appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
                float percent = 100*Math.abs(i)/appBarLayout.getTotalScrollRange();
                if(percent == 0){
                    percent = 0.1f;
                }
                findViewById(R.id.title).setAlpha(percent/100);
            }
        });

        tvPrice.setOnClickListener(this);
        tvNewProduct.setOnClickListener(this);
        tvHotProduct.setOnClickListener(this);
        ivMenu.setOnClickListener(this);
        icBack.setOnClickListener(this);
        rightIcon.setOnClickListener(this);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2);
        recycleview.setLayoutManager(gridLayoutManager);
        GridSpacingItemDecoration itemDecoration = new GridSpacingItemDecoration(2,20,true);
        recycleview.addItemDecoration(itemDecoration);
        shopDetailAdapter = new ShopDetailAdapter(R.layout.product_item,newProductData);
        shopDetailAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                switch (currentSelect){
                    case NEW_PRODUCT:
                        if(canLoadMoreNew) loadNewProductData(null);
                        else shopDetailAdapter.loadMoreEnd();
                        break;
                    case HOT_PRODUCT:
                        if(canLoadMoreHot) loadHotProduct(null);
                        else shopDetailAdapter.loadMoreEnd();
                        break;
                    case PRICE_UP:
                        if(canLoadMoreUp) loadPriceUpData(null);
                        else shopDetailAdapter.loadMoreEnd();
                        break;
                    case PRICE_DOWN:
                        if(canLoadMoreDown)loadPriceDownData(null);
                        else shopDetailAdapter.loadMoreEnd();
                        break;
                }
            }
        },recycleview);
        shopDetailAdapter.setEmptyView(R.layout.normal_empty_view);
        recycleview.setAdapter(shopDetailAdapter);

        shopDetailAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                HomeBaseBean homeBaseBean = null;
                switch (currentSelect){
                    case NEW_PRODUCT:
                        homeBaseBean = newProductData.get(position);
                        homeBaseBean.setId(newProductData.get(position).getGoods_id());
                        break;
                    case HOT_PRODUCT:
                        homeBaseBean = hotProductData.get(position);
                        homeBaseBean.setId(hotProductData.get(position).getGoods_id());
                        break;
                    case PRICE_UP:
                        homeBaseBean = priceUpData.get(position);
                        homeBaseBean.setId(priceUpData.get(position).getGoods_id());
                        break;
                    case PRICE_DOWN:
                        homeBaseBean = priceDownData.get(position);
                        homeBaseBean.setId(priceDownData.get(position).getGoods_id());
                        break;
                }
                homeBaseBean.setType(Const.PRODUCT);
                Const.jumpHandle(mActivity,homeBaseBean);
            }
        });

        tvCollect.setOnClickListener(this);
        tvConnect.setOnClickListener(this);
    }

    private void resetAllTab(){
        int color = getResources().getColor(R.color.color_text_black5nd);

        tvNewProduct.setTextColor(color);
        tvHotProduct.setTextColor(color);
        tvPrice.setTextColor(color);

        tvNewProduct.setBackground(null);
        tvHotProduct.setBackground(null);
        tvPrice.setBackground(null);

    }

    private void setSelect(int select){
        resetAllTab();
        int color = getResources().getColor(R.color.color_red);
        Drawable drawable = getResources().getDrawable(R.drawable.under_line_red);
        currentSelect = select;
        switch (select){
            case NEW_PRODUCT:
                tvNewProduct.setTextColor(color);
                tvNewProduct.setBackground(drawable);
                if(newProductData.size() == 0){
                    loadNewProductData(this);
                }
                shopDetailAdapter.setNewData(newProductData);
                break;

            case HOT_PRODUCT:
                tvHotProduct.setTextColor(color);
                tvHotProduct.setBackground(drawable);
                if(hotProductData.size() == 0){
                    loadHotProduct(this);
                }
                shopDetailAdapter.setNewData(hotProductData);
                break;

            case PRICE_DOWN:
                tvPrice.setTextColor(color);
                tvPrice.setBackground(drawable);
                if(priceDownData.size() == 0){
                    loadPriceDownData(this);
                }
                shopDetailAdapter.setNewData(priceDownData);
                break;

            case PRICE_UP:
                tvPrice.setTextColor(color);
                tvPrice.setBackground(drawable);
                if(priceUpData.size() == 0){
                    loadPriceUpData(this);
                }
                shopDetailAdapter.setNewData(priceUpData);
                break;
        }
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.tv_new_product:
                tvPrice.setCompoundDrawables(null,null,null,null);
                setSelect(NEW_PRODUCT);
                break;
            case R.id.tv_hot_product:
                tvPrice.setCompoundDrawables(null,null,null,null);
                setSelect(HOT_PRODUCT);
                break;
            case R.id.tv_price:
                if(currentSelect == PRICE_DOWN){
                    setSelect(PRICE_UP);
                    Drawable up = getResources().getDrawable(R.drawable.up);
                    up.setBounds(0, 0, up.getMinimumWidth(), up.getMinimumHeight());
                    tvPrice.setCompoundDrawables(null,null,up,null);
                }else{
                    setSelect(PRICE_DOWN);
                    Drawable down = getResources().getDrawable(R.drawable.down);
                    down.setBounds(0, 0, down.getMinimumWidth(), down.getMinimumHeight());
                    tvPrice.setCompoundDrawables(null,null,down,null);
                }
                break;
            case R.id.iv_menu:
                showCategory();
                break;
            case R.id.ic_back:
                finish();
                break;
            case R.id.right_icon:
                ToastUtil.showToast("搜索");
                break;
            case R.id.tv_collect:
                ToastUtil.showToast("添加喜欢店铺！");
                likeshop();
                break;
            case R.id.tv_connect:
                ToastUtil.showToast("拨打电话！");
                break;
        }
    }

    private void cacelLike() {
        Map<String, Object> param = NetTool.newParams();
        param.put("store_id",shopId);
        NetTool.getApi().delLikeShop(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<OperateResponseBean3>>() {
                    @Override
                    public void onData(BaseResponse<OperateResponseBean3> value) {
                        if(value.getCode() == 0 && value.getData().isSuccess()){
                            tvCollect.setText("关注");
                            bean.setFocus(true);
                            ToastUtil.showToast("操作成功！");
                        }else{
                            ToastUtil.showToast(value.getData().getMessage());
                        }
                    }
                });
    }

    private void likeshop() {
        Map<String, Object> param = NetTool.newParams();
        param.put("shop_id",shopId);
        NetTool.getApi().likeshop(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<OperateResponseBean3>>(this) {
                    @Override
                    public void onData(BaseResponse<OperateResponseBean3> value) {
                        if(value.getCode() == 0 && value.getData()!=null){
                            ToastUtil.showToast(value.getData().getMessage());
                            if(value.getData().isSuccess()){
                                tvCollect.setText("已关注");
                                tvCollect.setClickable(false);
                            }
                        }
                    }
                });
    }

    private void showCategory() {
        final NiceDialog dialog = NiceDialog.init();
        dialog.setLayoutId(R.layout.category)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    protected void convertView(ViewHolder viewHolder, BaseNiceDialog baseNiceDialog) {
                        RecyclerView recyclerView = (RecyclerView) viewHolder.getView(R.id.rv_category);
                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ShopDetailActivity.this);
                        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                        recyclerView.setLayoutManager(linearLayoutManager);

                        CategoryAdapter adapter = new CategoryAdapter(R.layout.category_item,bean.getCategoryItems());
                        recyclerView.setAdapter(adapter);
                        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                resetAllFlag();
                                for(CategoryItem item:bean.getCategoryItems()){
                                    item.setSelected(false);
                                }
                                bean.getCategoryItems().get(position).setSelected(true);
                                nowCategory = bean.getCategoryItems().get(position);
                                adapter.notifyDataSetChanged();
                                dialog.dismiss();
                                setSelect(currentSelect);
                            }
                        });

                    }
                })
                .setAnimStyle(R.style.dialogAnim)
                .setGravity(Gravity.RIGHT)
                .setWidth(280)
                .setHeight(WindowManager.LayoutParams.MATCH_PARENT)
                .setOutCancel(true)
                .show(getSupportFragmentManager());
    }

    private class ShopDetailAdapter extends BaseQuickAdapter<ShopDetailProductItem,BaseViewHolder>{

        public ShopDetailAdapter(int layoutResId, @Nullable List<ShopDetailProductItem> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, ShopDetailProductItem item) {
            Glide.with(mActivity).load(item.getGoods_img()).into((ImageView) helper.getView(R.id.product_img));
            helper.setText(R.id.product_title,item.getGoods_name());
            helper.setText(R.id.product_sales,item.getSales_volume() + " " +item.getGoods_number());
            helper.setText(R.id.product_price,item.getShop_price()+"元");
        }
    }

    private class CategoryAdapter extends BaseQuickAdapter<CategoryItem,BaseViewHolder>{

        public CategoryAdapter(int layoutResId, @Nullable List<CategoryItem> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, CategoryItem item) {
            helper.setText(R.id.tv_category,item.getText());
            if(item.isSelected()){
                helper.setTextColor(R.id.tv_category,getResources().getColor(R.color.color_red));
            }else{
                helper.setTextColor(R.id.tv_category,getResources().getColor(R.color.color_text_black5nd));
            }
        }
    }
}
